package uk.co.q3c.deplan.client.ui.component;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Widget;

public class RichTextEditor {

	private DisclosurePanel backPanel;
	private RichTextToolbar toolbar;
	private RichTextArea richTextArea;
	private Label editSwitch;
	private VerticalFlowPanel mainPanel;

	/**
	 * 
	 */
	public RichTextEditor() {
		super();
		construct();
		layout();
	}

	private void layout() {
		backPanel.setOpen(true);
		mainPanel.setSize("800px", "");
		backPanel.setContent(mainPanel);
		richTextArea.setSize("100%", "100px");
		richTextArea.addStyleName("richTextArea");
		mainPanel.add(toolbar);
		toolbar.setWidth("");
		mainPanel.add(richTextArea);
		mainPanel.add(editSwitch());
		editSwitch.setWidth("100%");
		toolbar.setVisible(false);

	}

	private Widget editSwitch() {
		editSwitch.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				toolbar.setVisible(!toolbar.isVisible());
				if (toolbar.isVisible()) {
					mainPanel.insert(toolbar, 0);
					editSwitch.setText("done");
				} else {
					mainPanel.remove(toolbar);
					editSwitch.setText("edit");
				}
				richTextArea.setEnabled(toolbar.isVisible());
			}
		});
		return editSwitch;
	}

	private void construct() {
		backPanel = new DisclosurePanel("Description");
		// backPanel.setWidth("560px");
		richTextArea = new RichTextArea();
		toolbar = new RichTextToolbar(richTextArea);
		mainPanel = new VerticalFlowPanel();
		mainPanel.addStyleName("richTextEditor");
		editSwitch = new Label("edit");
	}

	public void setText(String text) {
		richTextArea.setText(text);
	}

	public Widget getWidget() {
		return backPanel;
	}

}
